<?php
namespace  Ekadmin\Controller;
use \Think\Controller;
class IndexController extends CommonController
{
        public  function index()
        {            
            session_start(); 
            $this->display();
        }


        public function gg()
        {
            echo json_encode($this->mainChar());
        }

        public function main()
        {



//            $this->assign('order_num_char',json_encode($this->mainChar()));
            $this->assign('usercount',$this->userCount());
            $this->assign('ordercount',$this->orderCount());                        
           $this->assign('orderjiner',$this->orderJiner());
//            $obj = M('reward_log');
//            $leiji = $obj->where(['uniacid'=>$this->acid,'status'=>1])->sum('amount');
//            $leijierror = $obj->where(['uniacid'=>$this->acid,'status'=>2])->sum('amount');
//            $this->assign('leiji',$leiji);
//            $this->assign('leijierror',$leijierror);
            $this->display();
        }

        /**         * @今日会员数和今日团长数         */
        private function userCount()
        {
            $endtime = strtotime(date("Y-m-d 00:00:00"));
            $map['addtime'] = array('gt',$endtime);
            $map['uniacid'] = $this->uniacid;
            $dayuser = M('user')->where($map)->count();
            $map2['addtime'] = array('gt',$endtime);                        
            $map2['is_tuan'] =2;
            $map2['uniacid'] = $this->uniacid;
            $daytuan = M('user')->where($map2)->count();
            return ['dayuser'=>$dayuser,'daytuan'=>$daytuan];
        }

        /**         * @今日小区数和今日订单数         */
        private function orderCount()    
        {    
            $endtime = strtotime(date("Y-m-d 00:00:00"));    
            $map['addtime'] = array('gt',$endtime);    
            $map['uniacid'] = $this->uniacid;    
            $dayuser = M('order')->where($map)->count();                            
    
            $map2['addtime'] = array('gt',$endtime);    
            $map2['uniacid'] = $this->uniacid;    
        
            $dayxiaoqu = M('xiaoqu')->where($map2)->count();    
            return ['dayorder'=>$dayuser,'dayxiaoqu'=>$dayxiaoqu];    
        }

        /**         * @今日团长佣金和今日营业额         */
        private function orderJiner()    
        {    
            $endtime = strtotime(date("Y-m-d 00:00:00"));    
            $map['addtime'] = array('gt',$endtime);    
            $map['uniacid'] = $this->uniacid;    
        
            $dayuser = M('rewardlog')->where($map)->sum('money');    
            $endtime2 = strtotime(date("Y-m-d 00:00:00"));    
            $map2['addtime'] = array('gt',$endtime2);    
            $map2['is_pay'] = 2;    
            $map2['uniacid'] = $this->uniacid;    
            $monthuser = M('order')->where($map2)->sum('total_jiner');    
            return ['daytuanJiner'=>$dayuser,'dayorderJiner'=>$monthuser];    
        }


    private function mainChar()
        {
            $timeNum=7;
            for($i=0;$i<$timeNum;$i++){
                $data['time'][$i]=date('Y-m-d',time()-($timeNum-$i)*24*3600);
                $data['order_num'][$i]=0;
                $data['order_amount'][$i]=0;
            }            
            $start_time= strtotime(date('Y-m-d 00:00:00', strtotime('-'.$timeNum.' days')));

            $map['addtime'] = array('between',[$start_time,time()]);
            $map['is_pay'] = 2;
            $map['uniacid'] = $this->uniacid;
            $Orderlist = M('order')->where($map)->field('id,total_jiner,addtime')->select();

            foreach ($Orderlist as $k => $v)
            {
                foreach ($data['time'] as $kk => $vv)
                {
                    if($vv == date('Y-m-d',$v['addtime'])){
                        $data['order_num'][$kk]++;
                        $data['order_amount'][$kk]+=$v['total_jiner'];
                        break;
                    }                    
                }
            }



            $order_num_echar["title"]["text"]='近7日订单统计';
            $order_num_echar["color"]=array('rgb(25, 183, 207)');
            $order_num_echar["tooltip"]["trigger"]='axis';
            $order_num_echar["tooltip"]["axisPointer"]["type"]='shadow';
            $order_num_echar["toolbox"]["show"]=true;
            $order_num_echar["toolbox"]["feature"]["mark"]["show"]=true;
            $order_num_echar["toolbox"]["feature"]["magicType"]["show"]=true;
            $order_num_echar["toolbox"]["feature"]["magicType"]["type"]=array('line', 'bar');
            $order_num_echar["toolbox"]["feature"]["restore"]["show"]=true;
            $order_num_echar["toolbox"]["feature"]["saveAsImage"]["show"]=true;
            $order_num_echar["legend"]["data"]=array('订单数量');
            $order_num_echar["xAxis"]["axisTick"]["length"]=7;
            $order_num_echar["xAxis"]["axisTick"]["interval"]=0;
            $order_num_echar["xAxis"]["axisTick"]["inside"]=false;
            $order_num_echar["xAxis"]["data"]=$data['time'];
            $order_num_echar["yAxis"]["minInterval"]=1;
            $series['name']='订单数量';
            $series['type']='bar';
            $series['data']=$data['order_num'];
            $series['barMaxWidth']=40;
            $series['label']["normal"]["show"]=true;
            $series['label']["normal"]["position"]='top';
            $order_num_echar["series"]=array($series);
            $data['order_num_eachar']=$order_num_echar;



            $order_amount_echar=$order_num_echar;
            $order_amount_echar["title"]["text"]='近'.$timeNum.'日营业额统计';
            $order_amount_echar["legend"]["data"]=array('营业收入');
            $order_amount_echar["yAxis"]["axisLabel"]["show"]=true;
            $order_amount_echar["yAxis"]["axisLabel"]["interval"]='auto';
            $order_amount_echar["yAxis"]["axisLabel"]["formatter"]='{value} 元';
            $order_amount_echar["yAxis"]["axisLabel"]["textStyle"]["color"]='#1e90ff';
            $order_amount_echar["yAxis"]["axisLabel"]["textStyle"]["fontFamily"]='verdana';
            $order_amount_echar["yAxis"]["axisLabel"]["textStyle"]["fontSize"]=10;
            $order_amount_echar["yAxis"]["axisLabel"]["textStyle"]["fontStyle"]='normal';
            $order_amount_echar["yAxis"]["axisLabel"]["textStyle"]["fontWeight"]='bold';
            $series['name']='营业收入';
            $series['data']=$data['order_amount'];
            $order_amount_echar["series"]=array($series);
            $data['order_amount_echar']=$order_amount_echar;
            return $data;
        }


}